package com.jsoft.jsofted.platform.service.impl;

import com.jsoft.jsofted.platform.entity.RoleEntity;
import com.jsoft.jsofted.platform.mapper.RoleMapper;
import com.jsoft.jsofted.framework.mybatis.CocQueryWrapper;
import com.jsoft.jsofted.framework.service.impl.CocServiceImpl;
import com.jsoft.jsofted.platform.service.RoleService;
import com.jsoft.jsofted.platform.vo.RoleBean;
import com.jsoft.jsofted.platform.vo.RoleTreeMaker;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleServiceImpl extends CocServiceImpl<RoleMapper, RoleEntity> implements RoleService {

    @Override
    public List<RoleBean> queryTree(Map queryParams) {
        if (queryParams == null) {
            queryParams = new LinkedHashMap();
        }

        CocQueryWrapper queryWrapper = this.parseQueryWrapper(queryParams);

        List list = list(queryWrapper);
        List tree = RoleTreeMaker.make(list);

        return tree;
    }

    protected CocQueryWrapper parseQueryWrapper(Map queryParams) {
        if (queryParams == null) {
            queryParams = new LinkedHashMap();
        }

        CocQueryWrapper queryWrapper = new CocQueryWrapper();
        this._parseSortParams(queryWrapper, queryParams);
        this._parseKeywordsParams(queryWrapper, queryParams);
        this._parseMenuParams(queryWrapper, queryParams);
        this._parseTenantParams(queryWrapper, queryParams);
        this._parseUserParams(queryWrapper, queryParams);

        return queryWrapper;
    }

    /**
     * 解析【条件参数】中的【用户参数】
     * 查询指定的用户拥有哪些角色？
     */
    private void _parseUserParams(CocQueryWrapper queryWrapper, Map queryParams) {
        String userId = (String) queryParams.get("userId");
        if (StringUtils.hasText(userId)) {
            queryWrapper.subSelect("id", "in",
                    "(select role_id from sys_user_role where user_id = {0})", userId);
        }
    }

    /**
     * 解析【条件参数】中的【租户参数】
     * 查询指定的租户拥有哪些角色？
     */
    private void _parseTenantParams(CocQueryWrapper queryWrapper, Map queryParams) {
        String tenantId = (String) queryParams.get("tenantId");
        if (StringUtils.hasText(tenantId)) {
            queryWrapper.subSelect("id", "in",
                    "(select role_id from sys_tenant_role where tenant_id = {0})", tenantId);
        }
    }

    /**
     * 解析【条件参数】中的【菜单参数】
     * 查询指定的菜单可以被哪些角色访问？
     */
    private void _parseMenuParams(CocQueryWrapper queryWrapper, Map queryParams) {
        String menuId = (String) queryParams.get("menuId");
        if (StringUtils.hasText(menuId)) {
            queryWrapper.subSelect("id", "in",
                    "(select role_id from sys_role_perm where menu_id = {0})", menuId);
        }
    }

    private void _parseSortParams(CocQueryWrapper queryWrapper, Map queryParams) {
        queryWrapper.orderByDesc("sort_value", "create_time");
    }

    /**
     * 解析【条件参数】中的【关键字参数】
     */
    private void _parseKeywordsParams(CocQueryWrapper queryWrapper, Map queryParams) {
        String keywords = (String) queryParams.get("keywords");
        if (StringUtils.hasText(keywords)) {
            queryWrapper.like("name", keywords);
        }
    }


}
